﻿<?php
require_once('custom/config.php');//конфигуратор сайта
//Подключение PEAR////////////////////////////////////////////
$include_path = ini_get('include_path');
$include_path .= $cfg['inc_path_delemiter'].$cfg['PEAR_path'];
//Подключение Zend Framework//////////////////////////////////
$include_path .= $cfg['inc_path_delemiter'].$cfg['fw_path'].'zend/';
ini_set('include_path',$include_path);
//////////////////////////////////////////////////////////////

require_once('fw_config.php');//конфигуратор framework'а

require_once('db.php');
require_once($cfg['fw_path'].'smarty/Smarty.class.php');
require_once($cfg['fw_path'].'lib/class.tplengine.php');
require_once($cfg['fw_path'].'lib/class.controller.php');
require_once($cfg['fw_path'].'lib/module.core.inc.php');
require_once($cfg['fw_path'].'lib/lib.php');
require_once($cfg['fw_path'].'lib/security.inc.php');

class PageController extends Controller {
   
    public $cache_id = 0;
    public $print_version = false;
    public $lastModified = '';
    //public $etag = '';
    function __construct()
    {
    	parent::__construct();
    	$this->tpl->caching = true;
    }
  
    /**
     * Точка входа в Framework
     * 
     * 
     * @access public
     */
    public function ShowPage($url)
    {
    	try {
    		$this->LoadModule('partition');
    		$partition = & 	$this->Modules['partition'];
    		//if (isset($_GET['print'])) {$this->print_version = true;}
    		$PageInfo = $partition->GetPage($url);
    		//print_r($PageInfo);
    		$this->cache_id = null;
    		if (	(isset($PageInfo[1])) &&
    			(isset($PageInfo[1]['extension'])) &&
    			($PageInfo[1]['extension']!=='')
    		)
    		{
    			//загружаем расширение
    			$this->LoadModule($PageInfo[1]['extension']);
    			$mod = $this->Modules[$PageInfo[1]['extension']];
    			$mod->GetPage($PageInfo,$url);
    		}
    		else {
    			//расширение по умолчанию
    			if (	(isset($PageInfo[0])) && ($PageInfo[0]['status']==404)	)
    			{
    				header ("HTTP/1.1 404 Not Found");
    				exit;
    			}
    			if ((isset($PageInfo[0]['publish']) && $PageInfo[0]['publish']==0)||(!isset($PageInfo[0]['publish'])))
    			{
    				header ("HTTP/1.1 404 Not Found");
    				exit;
    			}
    			$this->tpl->assign('title',$PageInfo[0]['title']);
    			$this->tpl->assign('description',$PageInfo[0]['description']);
    			$this->tpl->assign('keywords',$PageInfo[0]['keywords']);
    			$this->main_template = $PageInfo[0]['template'];
    			$_file = $this->cfg['absolute_path'].FW_PAGES_DIR.$PageInfo[0]['pageName'];
    			$this->content_template = $_file;
    			$this->tpl->assign("incFile",1);
    			$this->tpl->assign("content",$this->content_template);
    			if ($PageInfo[0]['cache']=='1') 
    			{
    				$this->lastModified = gmdate ('D, d M Y H:i:s', filemtime($_file));
    				//$this->etag = $PageInfo[0]['hash'].$this->lastModified;
    				$this->cache_id = $PageInfo[0]['id'];
    				$this->tpl->caching = true;
    			}
    			else $this->cache_id = null;
    		}
    		$_NotModified = false;
    		if ($this->lastModified!='')
    		{
    			$this->lastModified.=' GMT';
	    		if (isset($_SERVER['HTTP_IF_MODIFIED_SINCE'])) 
    			{
    				$if_modified_since = preg_replace('/;.*$/', '', $_SERVER['HTTP_IF_MODIFIED_SINCE']);
    				if ($if_modified_since == $this->lastModified) 
    				{
    					header('HTTP/1.1 304 Not Modified');
    					$_NotModified = true;
  					}
  				}
				header('Cache-Control: max-age=86400, must-revalidate');
	   			header('Last-Modified: '.$this->lastModified);
	   			header('Expires: '.gmdate ('D, d M Y H:i:s', time()+86400).' GMT');
	   			if ($_NotModified) return;
    		}
    		/*$str = urlencode("Отопительные радиаторы, установка радиаторов отопления Москва");
    		echo $str;
    		echo "<br>123<br><br>";
    		echo urldecode($str);*/
    		if (isset($_GET[FW_URL_TITLE_VARNAME])) $this->tpl->assign('title', PrepareVar_3( iconv("cp1251","UTF-8",$_GET[FW_URL_TITLE_VARNAME])));
    		
    		//Отображение...
    		$this->tpl->assign("baseURL",$this->cfg['absolute_url']);
    		if (is_null($this->cache_id))
    		{
    			$this->tpl->caching = false;
    			if (!isset($_GET['ajax']))
    				$this->tpl->display($this->main_template);
    			else
    				$this->tpl->display($this->content_template);
    		}
    		else{
    			//$this->tpl->caching = true;
    			//if ($this->print_version) {	$this->cache_id='pv_'.$this->cache_id;}
    			if (!isset($_GET['ajax']))
    			{
    				$this->tpl->display($this->main_template,$this->cache_id);
    			}
    			else
    				$this->tpl->display($this->content_template,$this->cache_id.'_ajax');
    		}
    	}
    	catch (fwException $e)
    	{
    		if (isset($this->cfg['debug']) && ($this->cfg['debug']))
    		{
    			echo $e->__toString();
    		}
    		else 
    		{
    			header ("HTTP/1.1 404 Not Found");
    			exit;
    		}
    	}
    }
	public function LoadModule($ModuleName)
	{
		$modName = strtolower($ModuleName);
		if ( (!isset($this->Modules[$modName]))
			|| (!is_a($this->Modules[$modName],$modName)))
		{
			//создадим тогда этот модуль и запихнем в список загруженных
			//echo $this->cfg['fw_path'].'modules/'.$modName.'/module.'.$modName.'.php';
			if (file_exists($this->cfg['fw_path'].'modules/'.$modName.'/module.'.$modName.'.php'))
			{
				require_once($this->cfg['fw_path'].'modules/'.$modName.'/module.'.$modName.'.php');
				$userMod = 'Module'.$modName;
				$mod = new $userMod($this);
				$this->Modules[$modName] = & $mod;
			}
			else 
			{
				throw new fwException("Не могу найти файл модуля: ".$ModuleName,0);
			}
		}
		return true;
	}
}

?>